package com.component;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@Component("rotationLoadBalancer")
public class RotationLoadBalancer implements LoadBalance{
    AtomicInteger atomicInteger=new AtomicInteger();
    public ServiceInstance getSingleAddres(List<ServiceInstance> serviceInstances) {
        int size = serviceInstances.size();
        if (size==0){
            throw new RuntimeException("当前无可用服务");
        }
        int index = atomicInteger.incrementAndGet() % size;
        ServiceInstance serviceInstance = serviceInstances.get(index);
        return serviceInstance;
    }
}
